package com.mvp.shq.washer.ui.shop.order.myorder;


import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.adapter.OrderMyAllListAdapter;
import com.mvp.shq.washer.base.BaseFragment;
import com.mvp.shq.washer.entity.shop.MyOrderRestult;
import com.mvp.shq.washer.entity.shop.result.MyOrderListRestult;
import com.mvp.shq.washer.entity.shop.result.OrderDataListResult;
import com.mvp.shq.washer.entity.shop.result.OrderDataResult;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.rxbus.Subscribe;
import com.mvp.shq.washer.utils.DataDictionary;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.JumpUtil;
import com.mvp.shq.washer.utils.RecyclerViewDivider;
import com.mvp.shq.washer.utils.StringUtils;
import com.mvp.shq.washer.view.widget.CustomDialog;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyOrderListFragment extends BaseFragment<Presenter, Model> implements MyOrderConstract.View {


    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private String orderStatue; //订单状态 0：未支付 1：待收货 2：待评价
    private List<OrderDataListResult> mRestultList;
    private int pageNo = 1;
    private int pageSize = 10;
    private boolean hasMoreData = true;
    private boolean isLoadMore = false;
    Map<String,String> map = new HashMap<String, String>();
    public MyOrderListFragment(String orderStatue) {
        this.orderStatue = orderStatue;
    }
    public MyOrderListFragment(){}
    private int itemPosition = 0;
    private CustomDialog mDialog;
    @Override
    public int getLayoutId() {
        return R.layout.fragment_category;
    }

    @Override
    public void initView() {
        recyclerview.setLayoutManager(new LinearLayoutManager(context));
        RecyclerViewDivider divider = new RecyclerViewDivider(context, LinearLayoutManager.HORIZONTAL, 10, ContextCompat.getColor(context, R.color.gregf8));
        recyclerview.addItemDecoration(divider);
        map.clear();
        if (!TextUtils.isEmpty(orderStatue)) {
            map.put("orderStatus", orderStatue);
        }
        presenter.getOrders(map,pageNo,pageSize);
        refreshLayout.setOnRefreshListener(refreshlayout -> {
            pageNo = 1;
            isLoadMore = false;
            hasMoreData = true;
            loadData();
        });
        refreshLayout.setOnLoadmoreListener(refreshlayout -> {
            pageNo ++;
            isLoadMore = true;
            if (hasMoreData){
                loadData();
            }else {
                refreshLayout.finishLoadmore();
            }
        });
    }

    private void loadData(){
        map.clear();
        if (!TextUtils.isEmpty(orderStatue)) {
            map.put("orderStatus", orderStatue);
        }
        presenter.getOrders(map,pageNo,pageSize);
    }
    @Override
    public void getOrders(MyOrderRestult myOrderRestult) {
        mRestultList= myOrderRestult.getList();
        OrderMyAllListAdapter orderMyAllListAdapter=new OrderMyAllListAdapter(context,mRestultList,myAllOrderItemOnClick);
        recyclerview.setAdapter(orderMyAllListAdapter);
        refreshLayout.finishLoadmore();
        refreshLayout.finishRefresh();
    }

    @Override
    public void orderTakeS() {
        refreshLayout.autoRefresh();
    }

    private OrderMyAllListAdapter.MyAllOrderItemOnClick myAllOrderItemOnClick = new OrderMyAllListAdapter.MyAllOrderItemOnClick() {
        @Override
        public void onItemOnClick(int position) {
            OrderDataListResult myOrderListRestult = mRestultList.get(position);
            String orderId = myOrderListRestult.getId();
            itemPosition = position;
            switch (myOrderListRestult.getOrderStatus()) {
                //待付款
                case DataDictionary.ORDERNOPAY:
                    JumpUtil.toOrderPay(context,0,mRestultList.get(position).getId());
                    break;
                //待收货
                case DataDictionary.ORDERTAKE:
                    JumpUtil.toOrderTake(context, orderId);
                    break;
                //待评价
                case DataDictionary.ORDEREVALUATE:
                    JumpUtil.toOrderDesc(context, orderId);
                    break;
            }
        }

        @Override
        public void onBtnClick(int position) {
            OrderDataListResult myOrderListRestult = mRestultList.get(position);
            String orderNo = myOrderListRestult.getOrderNo();
            itemPosition = position;
            Logger.d("点击item类型:"+myOrderListRestult.getOrderStatus());
            switch (myOrderListRestult.getOrderStatus()) {
                //付款
                case DataDictionary.ORDERNOPAY:
                    JumpUtil.toOrderPay(context,0,myOrderListRestult.getId());
                    break;
                //确认收货
                case DataDictionary.ORDERTAKE:
                    if (mDialog == null) {
                        CustomDialog.Builder builder = new CustomDialog.Builder(context);
                        mDialog = builder.setMessage("是否确认收货")
                                .setNegativeButton("确认", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        mDialog.dismiss();
                                        presenter.orderTake(orderNo);
                                    }
                                })
                                .setPositiveButton("取消", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        mDialog.dismiss();
                                    }
                                }).createTwoButtonDialog();
                    }
                    mDialog.show();
                    break;
                //评价
                case DataDictionary.ORDEREVALUATE:
                    RxBus.get().send(EventCode.SHOP_SAVE_ORDER_RESULT,mRestultList.get(itemPosition));
                    JumpUtil.toOrderProductComment(context);
                    break;
            }
        }
    };

    @Subscribe(code = EventCode.SHOP_SEND_ORDER_FRO_COMMENT)
    public void sendOrderToComment() {
        RxBus.get().send(EventCode.SHOP_GET_ORDER_FRO_COMMENT, mRestultList.get(itemPosition));
    }

    @Subscribe(code = EventCode.SHOP_REFRESH_ORDER_LIST)
    public void refreshList(){
        refreshLayout.autoRefresh();
    }
}
